package com.jintian.smart.kernel.autoconfigure.model;

import com.jintian.smart.kernel.common.spring.BeanSelfAware;
import lombok.extern.slf4j.Slf4j;
import org.beetl.ext.spring6.BeetlGroupUtilConfiguration;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

/**
 * 全局自定义bean处理器
 */
@Slf4j
@Order(100)
@Configuration(proxyBeanMethods = false)
public class GlobalCustomBeanProcessor extends BeetlGroupTemplateProcesser implements BeanPostProcessor, ApplicationContextAware {
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//        if (bean instanceof BeetlGroupUtilConfiguration) {
//            processBeetlGroupTemplate(this.applicationContext, (BeetlGroupUtilConfiguration) bean);
//        } else
        if (bean instanceof BeanSelfAware) {
            // 如果Bean实现了BeanSelfAware标识接口，就将代理对象注入 ，即使是prototype Bean也可以使用此种方式
            ((BeanSelfAware) bean).setSelf(bean);
        }
        return bean;
    }
}
